package com.naiterui.ehp.bp.support.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppErrorController implements ErrorController {

    @RequestMapping("/error")
    public ResponseVO<Void> handleError(HttpServletRequest request) {
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == 401) {
            return PublicService.returnResponseVO(BaseExceptionCodes.TOLOGIN);
        } else if (statusCode == 404) {
            return PublicService.returnResponseVO(BaseExceptionCodes.INVALID_REQUEST);
        } else if (statusCode == 403) {
            return PublicService.returnResponseVO(BaseExceptionCodes.INVALID_REQUEST);
        } else {
            return PublicService.returnResponseVO(BaseExceptionCodes.FAILED);
        }

    }

    // 新版本已处理
    // @Override
    // public String getErrorPath() {
    // return "/error";
    // }
}
